//+------------------------------------------------------------------+
//|                                                      MAalert.mq4 |
//|                                                               ss |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#import "mt4emailbridge.dll"
   int SendEmail(string SMTPServer, int SMTPPort, string SMTPUsername, string SMTPPassword, int RequiresSSL, string EmailFromAddress, string EmailFromName, string EmailToAddress, string EmailToName, string CCRecipient, string BCCRecipient, string Subject, string Body, int IsHtmlEmail, string AttachedFiles, int Flags);
#import
#property copyright "Copyright ?2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_buffers 2
#property  indicator_color1  Blue
#property  indicator_color2  Red
#property  indicator_width1  1
#property  indicator_width2  1
extern int ATRPeriod=14;
extern int MAPeriod=14;
extern int MAMODE=MODE_EMA;
extern int PriceMode=PRICE_CLOSE;
extern int MAShift=0;
extern int MAPeriod1=5;
extern int MAPeriod2=10;
extern int ADXPeriod=14;
extern int ADXPriceMode=PRICE_CLOSE;
extern double ADXCustomLevel=20.0;
//----
extern bool ATRCrossAlertEnable=true;
extern bool MACrossAlertEnalbe=true;
extern bool ADXCrossAlertEnalbe=true;
extern string ATRCrossUpAlertMessage = "ATRCrossUpMA";
extern string ATRCrossDownAlertMessage = "ATRCrossDownMA";
extern string MACrossUpAlertMessage = "MA1CrossUpMA2";
extern string MACrossDownAlertMessage = "MA1CrossDownMA2";
extern string ADXCrossAlertMessage= "ADXCrossUP";
extern string AlerSoundFile="alert.wav";
extern bool   PopupAlert = false; 
extern bool   SoundAlert = true; 
extern bool   EmailAlert = true; 
extern string EmailAddr="mingxinhe@gmail.com";
extern string Password="YourPassword";
extern string SMTPServer="smtp.gmail.com";
extern int    SMTPPort=587;




//---- buffers
double ATRBuffer[];
double MaBuffer[];
//----
int ExtCountedBars=0;
datetime lastAlertTime;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
int init()
  {
//---- indicators
  //lastAlertTime=0;
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   //IndicatorDigits(Digits);
  //---- indicator buffers mapping
   SetIndexBuffer(0,ATRBuffer);
   SetIndexBuffer(1,MaBuffer);
   
   SetIndexLabel(0,"ATR");
   SetIndexLabel(1,"MA");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   double atr1,atr2, ma1,ma2,ma11,ma12,ma21,ma22,adx1,adx2;
   string message,body;

   //ArraySetAsSeries(ATRBuffer,true);

   int limit;
   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- ATR counted in the 1-st buffer
   for(int i=0; i<limit; i++)
      ATRBuffer[i]=iATR(NULL,0,ATRPeriod,i);
//---- ma counted in the 2-nd buffer
   for(i=0; i<limit; i++)
      MaBuffer[i]=iMAOnArray(ATRBuffer,Bars,MAPeriod,MAShift,MAMODE,i);
//---- done

   //if have sent alert and email then return
   if(lastAlertTime>=Time[0]) return;
   atr1=ATRBuffer[1];
   atr2=ATRBuffer[2];
   ma1=MaBuffer[1];
   ma2=MaBuffer[2];
   //Print(ma1," ma ",ma2);  
   if(ATRCrossAlertEnable)
   {
      if(atr1>=ma1 && atr2<ma2)//atr crossup ma
      {
         //sent alert atr crossup ma
         message=Symbol() + " "+ Period() +": "+ATRCrossUpAlertMessage;
         body=Symbol() + " "+ Period() +": "+"ATR has crossed up Ma!";
         if (SoundAlert) PlaySound(AlerSoundFile);         
         if (PopupAlert) Alert(message);
         //sent email atr crossup ma
         if (EmailAlert) Print("send email result:"+sendEmail(message,body));
      
      }
       if(atr1<=ma1 && atr2>ma2)//atr crossdown ma
      {
         //sent alert atr crossdown ma
         message=Symbol() + " "+ Period() +": "+ATRCrossDownAlertMessage;
         body=Symbol() + " "+ Period() +": "+"ATR has crossed down Ma!";
         if (SoundAlert) PlaySound(AlerSoundFile);
         if (PopupAlert) Alert(message);
         //sent email atr crossdown ma
         if (EmailAlert)
         {
             Print("send email result:"+sendEmail(message,body));
         }
      }
   }
   
   if(MACrossAlertEnalbe)
   {
      ma11=iMA(NULL,0,MAPeriod1,MAShift,MAMODE,PriceMode,1);
      ma21=iMA(NULL,0,MAPeriod2,MAShift,MAMODE,PriceMode,1);
      ma12=iMA(NULL,0,MAPeriod1,MAShift,MAMODE,PriceMode,2);
      ma22=iMA(NULL,0,MAPeriod2,MAShift,MAMODE,PriceMode,2);
      
      if(ma11>=ma21 && ma12<ma22)//ma1 crossup ma2
      {
        //sent alert ma1 crossup ma2
        message=Symbol() + " "+ Period() +": "+MACrossUpAlertMessage;
        body=Symbol() + " "+ Period() +": "+"Ma1 has crossed up Ma2!";
        if (SoundAlert) PlaySound(AlerSoundFile);
        if (PopupAlert) Alert(message);
        //sent email ma1 crossup ma2
        if (EmailAlert) Print("send email result:"+sendEmail(message,body));
      }
      if(ma11<=ma21 && ma12>ma22)//ma1 crossdown ma2
      {
        //sent alert ma1 crossdown ma2
        message=Symbol() + " "+ Period() +": "+MACrossDownAlertMessage;
        body=Symbol() + " "+ Period() +": "+"Ma1 has crossed down Ma2!";
        if (SoundAlert) PlaySound(AlerSoundFile);
        if (PopupAlert) Alert(message);
        //sent email ma1 crossdown ma2
        if (EmailAlert) Print("send email result:"+sendEmail(message,body));
     }
   }
    if(ADXCrossAlertEnalbe)
    {
      adx1=iADX(NULL,0,ADXPeriod,PriceMode,MODE_MAIN,1);
      adx2=iADX(NULL,0,ADXPeriod,PriceMode,MODE_MAIN,2);
      
      if(adx1>=ADXCustomLevel && adx2<ADXCustomLevel)//adx crossup ADXCustomLevel
      {
        //sent alert adx crossup ADXCustomLevel
        message=Symbol() + " "+ Period() +": "+ADXCrossAlertMessage;
        body=Symbol() + " "+ Period() +": "+"ADX has crossed up "+ADXCustomLevel+"!";
        if (SoundAlert) PlaySound(AlerSoundFile);
        if (PopupAlert) Alert(message);
        //sent email adx crossup ADXCustomLevel
        if (EmailAlert) Print("send email result:"+sendEmail(message,body));
      }
    }
   
   
   lastAlertTime=Time[0];
//----
   return(0);
  }

int sendEmail(string subject, string body)
{
    int rst = SendEmail(SMTPServer, SMTPPort, EmailAddr, Password,
    1, EmailAddr, "ATR3MA", EmailAddr, "ATR3MA", "", "",
    subject, body, 0, 
    "", 1);
    return (rst);
}
  
//+------------------------------------------------------------------+